package org.hxzon.hdyn;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HGlobal {

    private Map<String, Object> vars;

    public HGlobal() {
        vars = new ConcurrentHashMap<String, Object>();
    }

    public Object var(String name) {
        return vars.get(name);
    }

    public HObject hvar(String name) {
        return (HObject) var(name);
    }

    public HFun fun(String name) {
        return (HFun) var(name);
    }

    public Collection<String> vars() {
        return vars.keySet();
    }

    //==========
    public void add(String name, Object obj) {
        vars.put(name, obj);
    }

    public Object remove(String name) {
        return vars.remove(name);
    }

    //============
    public HObject createVar(String name) {
        HObject r = new HObject(this);
        add(name, r);
        return r;
    }

    //============
    @SuppressWarnings("unchecked")
    public <T> T varToT(String name, Class<T> klass) {
        return (T) var(name);
    }

    @SuppressWarnings("unchecked")
    public <T> Map<String, T> varToMap(String name, Class<T> klass) {
        return (Map<String, T>) var(name);
    }

    @SuppressWarnings("unchecked")
    public <T> List<T> varToList(String name, Class<T> klass) {
        return (List<T>) var(name);
    }

    @SuppressWarnings("unchecked")
    public <T> T[] varToArray(String name, Class<T> klass) {
        return (T[]) var(name);
    }
}
